<?php
namespace app\api\controller\new;

use think\Controller;
use think\Request;
use think\Db;
use app\common\model\power\Rolepower;

class ProductOrder extends Controller
{
    public function initialize()
    {
        parent::initialize();
        $this->is_login();
        Rolepower::powerguard('305003'); // 订单管理权限
    }

    private function is_login()
    {
        $data = \app\common\model\user\User::is_sessionlogin();
        if (!$data['code']) {
            $data['options'] = 'relogin';
            json($data)->send();
            exit;
        }
    }
    // 生成订单（305003）
    public function productOrder(Request $request)
    {
        $loginUser = session('user');
        if (!$loginUser || $loginUser['role'] != 305) {
            return json(['code'=>0, 'msg'=>'无权限']);
        }
        $data = $request->post();
        if (empty($data['contract_id']) || empty($data['devices_id']) || empty($data['customer_id'])) {
            return json(['code'=>0, 'msg'=>'参数不完整']);
        }
        $orderData = [
            'contract_id' => $data['contract_id'],
            'devices_id' => json_encode($data['devices_id']),
            'factory_id' => $data['factory_id'] ?? 0,
            'customer_id' => $data['customer_id'],
            'track_number' => $data['track_number'] ?? '',
            'status' => 1,
            'create_time' => time(),
            'update_time' => time()
        ];
        Db::name('contract_order')->insert($orderData);
        return json(['code'=>1, 'msg'=>'生成订单成功']);
    }

    // 订单列表（305003）
    public function productOrderList(Request $request)
    {
        $loginUser = session('user');
        if (!$loginUser || $loginUser['role'] != 305) {
            return json(['code'=>0, 'msg'=>'无权限']);
        }
        $group_id = Db::name('user_group')->where('user_id', $loginUser['id'])->value('group_id');
        if (!$group_id) {
            return json(['code'=>0, 'msg'=>'未绑定销售组']);
        }
        $user_ids = Db::name('user_group')->where('group_id', $group_id)->column('user_id');
        $customer_ids = Db::name('user_customer')->whereIn('user_id', $user_ids)->column('customer_id');
        $orders = Db::name('contract_order')->whereIn('customer_id', $customer_ids)->select();
        foreach ($orders as &$order) {
            $order['device'] = Db::name('device')->whereIn('id', json_decode($order['devices_id'], true))->select();
            $order['customerInfo'] = Db::name('customer')->where('CustomerId', $order['customer_id'])->find();
            $order['contractInfo'] = Db::name('product_contract')->where('id', $order['contract_id'])->find();
        }
        return json(['code'=>1, 'msg'=>'success', 'data'=>$orders]);
    }

    // 订单状态修改（305003）
    public function productOrderChange(Request $request)
    {
        $loginUser = session('user');
        if (!$loginUser || $loginUser['role'] != 305) {
            return json(['code'=>0, 'msg'=>'无权限']);
        }
        $data = $request->post();
        if (empty($data['id']) || empty($data['track_number']) || empty($data['status'])) {
            return json(['code'=>0, 'msg'=>'参数不完整']);
        }
        $order = Db::name('contract_order')->where('id', $data['id'])->find();
        if (!$order) {
            return json(['code'=>0, 'msg'=>'订单不存在']);
        }
        Db::name('contract_order')->where('id', $data['id'])->update([
            'track_number' => $data['track_number'],
            'status' => $data['status'],
            'update_time' => time()
        ]);
        return json(['code'=>1, 'msg'=>'修改成功']);
    }
} 